from credit_voice_service.dialogue.entity import dialogue_session_entity
from credit_voice_service.dialogue.service.dialogue_storage import dialogueStorage
from credit_voice_service.utils.database_utils import DatabaseUtils
from credit_voice_service.settings import log


def get_entity_labels(session_id, workspace_id):
    result_labels = []
    # 1.从数据库获取数据
    database = DatabaseUtils()
    dialogue_labels = database.get_entity_labels(workspace_id)
    if dialogue_labels is None or len(dialogue_labels) < 1:
        return result_labels

    # 2.从redis获取用户对话记录
    dialogueSession = dialogueStorage.get_dialogue_session(session_id, workspace_id)
    dialogueItemList = dialogue_session_entity.getAllDialogueHistory(dialogueSession)
    if dialogueItemList is None:
        return result_labels
    all_label = dialogue_session_entity.get_all_labels(dialogueItemList)

    for dialogue_label in dialogue_labels:
        standard_label = dialogue_label['label']
        if "实体" in standard_label:
            for cur_label in all_label:
                if standard_label not in cur_label:
                    continue
                result_label = {"node": dialogue_label["node"], "label": standard_label}
                result_labels.append(result_label)
        else:
            if standard_label in all_label:
                result_label = {"node": dialogue_label["node"], "label": standard_label}
                result_labels.append(result_label)

    print(result_labels)
    log.info("result_labels：{}".format(result_labels))
    return result_labels


if __name__ == '__main__':
    get_entity_labels("0-2147-295117411-1684178205243699200-000", 347)
    pass
